﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FontTool
{
    public class commandGlyphManager
    {
        public List<commandGlyph> commandStack = new List<commandGlyph>();
        int index = -1;
        int stackCount = 0;
        bool refresh = false;

        public void executeCommand(nCharacter nchar, commandGlyph cmd)
        {
            refreshRequired();

            cmd.Execute(nchar);
            commandStack.Add(cmd);

            if (refresh)
            {
                commandStack.Clear();
                stackCount = 0;
                index = -1;
                refresh = false;
            }
            stackCount++;
            index++;
        }

        public void Undo(nCharacter nchar)
        {
            if (index >= 0)
            {
                commandGlyph cmd = commandStack[index];
                cmd.Undo(nchar);

                if (index > 0)
                    index--;
            }
        }

        public void Redo(nCharacter nchar)
        {
            if (index < commandStack.Count && index >=0)
            {
                commandGlyph cmd = commandStack[index];
                cmd.Redo(nchar);

                if (index < commandStack.Count - 1)
                    index++;
            }
        }

        void refreshRequired()
        {
            if (index + 1 != stackCount)
                refresh = true;
        }
    }
}
